#coding=utf-8
import os
from app import app
from flask import request
from flask import redirect
from flask import render_template
from flask import send_from_directory
from flask import url_for
from sdb import checkUser
from sdb import getapplist
from werkzeug.utils import secure_filename
import requests
@app.route("/favicon.ico")
def favicon():
    return send_from_directory(os.path.join(app.root_path,'static'),'img/favicon.ico',mimetype='image/vnd.microsoft.icon')
@app.route("/")
def index():
    return render_template('login.html')
@app.route("/",methods=['POST','GET'])
def login():
    if request.method=="POST":
        username = request.form['username']
        password = request.form['password']
        result = checkUser(username,password)
        if result == 1:
            return render_template('index.html')
        elif result == 0:
            return render_template('login.html')
@app.route('/<name>')
def route_select(name):
    if name=="tab-panel":
        return render_template('tab-panel.html')
    elif name=="chart":
        return render_template('chart.html')
    elif name=="table":
        return render_template('table.html')
    elif name=="form":
        return render_template('form.html')
    elif name=="blank":
        return render_template('blank.html')
    elif name=="index":
        return render_template('index.html')
@app.route('/upload',methods=['GET','POST'])
def upload():
    ALLOWED_EXTENSIONS = set(['apk'])
    if request.method == "POST":
        file = request.files['upfile']
        print file.filename
    return redirect('/ui')
# def allowed_file(filename):
#     return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

#     pass
@app.route('/ui',methods=['GET'])
def appinfo():
    applist=getapplist()
    return render_template('ui.html',applist=applist)